package com.qf.service;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.util.List;

/**
 *
 * <bean id="us" class="com.qf.service.UserServiceImpl>
 *
 *     </bean>
 * @author lixu
 */
//@Service("us")
@Service   //默认是接口的名称头字母小写  来定义id = userService
@Transactional//(propagation = Propagation.SUPPORTS)   // 控制整个类中所有的方法 （不包含查询）
public class UserServiceImpl implements UserService {


    /**
     *     注入的类型：有以下三种
     *      *   1、set
     *      *   2、构造
     *      *   3、注解注入
     *      *      -3.1- 通过类型注入
     *      *      -3.2-通过ID注入
     *
     *
     * @Autowired
     * 自动注入：
     *   1、通过类型注入    如果出现二个相同类型的
     *   UserMapper userMapper = ac.getBean(UserMapper.class);
     *   2、通过ID注入
     *   UserMapper userMapper = (UserMapper)ac.getBean("userMapper");
     *
     *   Spring全家桶 全套东西都使用Spring公司的东西

     */
    @Autowired
    //@Qualifier(value = "userMapper") //写正确的ID
    private UserMapper userMapper;
    //set 构造器进行注入

    /**
     * 自动注入
     * @Resource(name = "userMapper"): 此方式不使用
     * import javax.annotation.Resource; JDK中提供的自动注入注解
     */
/*    @Resource(name = "userMapper")
    private UserMapper um;*/


    /**
     * 事务注解
     *   0、四大特性：一致性 原子性 持久化  隔离性
     *   1、隔离级别 isolation = Isolation.DEFAULT
     *   2、传播行为 propagation = Propagation.REQUIRED
     *   3、超时时间  timeout = -1  :  -1:没有超时时间   5：单位是s
     *   4、扩展回滚异常
     *        rollbackForClassName = {"java.io.FileNotFoundException","xxxxxx"} //包名+类名
     *        rollbackFor = {FileNotFoundException.class,ArithmeticException.class}//类的Class
     *   5、减少回滚异常
     *        noRollbackForClassName = {"java.io.FileNotFoundException",""},//包名+类名
     *        noRollbackFor = {FileNotFoundException.class,ArithmeticException.class}//类的Class
     *   6、只能读 （查询）  readOnly = true
     */
    @Transactional(
             isolation = Isolation.DEFAULT,
             propagation = Propagation.REQUIRED,
             timeout = -1,
             rollbackForClassName = {"java.io.FileNotFoundException"},
             rollbackFor = {FileNotFoundException.class},
             noRollbackForClassName = {"java.io.FileNotFoundException"},
             noRollbackFor = {FileNotFoundException.class}//,
             //readOnly = true
    )
    public void insertUser(User user){

        //用户集合
       userMapper.insertUser(user);
       //int i = 1/0;
       userMapper.updateUserById(user);
    }

    /**
     * 练习：Aop切面注解
     * @param uid
     * @return
     */
    @Override
    public List<User> queryUser(Integer uid) {
        System.out.println("====我是核心代码");
        //int i = 1/0;
        return userMapper.findAll();
    }

    public int count(){
        return 12;
    }


}
